package edu.spec.student.dao.impl;

import edu.spec.student.dao.iface.IEventDao;
import edu.spec.student.model.event.Event;
import edu.spec.basic.model.enumeration.Status;
import org.hibernate.Query;
import org.hibernate.Session;
import org.madgecko.basic.dao.BaseDao;
import org.madgecko.basic.kit.BasicKit;
import org.madgecko.basic.model.Pager;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * Created by MadFrog on 2017/5/20.
 */
@Repository
public class EventDao extends BaseDao<Event> implements IEventDao {
    @Override
    public Pager<Event> findByCon(Status status, String keyword) {
        String hql = " from Event e where 1=1 ";
        if(status!=null){
            hql += " and e.status=" + status.ordinal();
        }
        if (!BasicKit.isEmpty(keyword)){
            hql += " and e.fullName like '%" + keyword + "%'";
        }
        hql += " order by e.id desc";
        return this.find(hql);
    }

    @Override
    public List<Event> listLatestEvent() {
        long current = System.currentTimeMillis();
        // 活动开始30分钟前结束报名
        current += 30 * 60 * 1000;
        Date latestDate = new Date(current);
        String hql = " from Event e where e.status=" + Status.ENABLED.ordinal() + " and e.eventDate>:latestDate";
        Session s = this.getSession();
        Query query = s.createQuery(hql.toString());
        query.setTimestamp("latestDate", latestDate);//注意这里，用setTimestamp﻿e，不要用setDate
        List<Event> latestList = query.list();
        return latestList;
    }

    @Override
    public void safeDelete(Integer id) {
        
    }

}
